package com.igoal.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.igoal.dto.UserDTO;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.UserAccountSettingsModel;

@Controller
public class UserAccountSettingsController extends BaseController {
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager) {
		this.userGoalManager = userGoalManager;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}
	
	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/userAccountSettings.html")
	public String show(ModelMap modelMap, HttpServletRequest request){		
		UserAccountSettingsModel model = new UserAccountSettingsModel();
		
		initLeftPage(model, request);
		initRightPage(model, request);
		initTopPage(model, request);
		
		applyModel(modelMap, model);
		
		return getViewPrefix() + "userAccountSettings";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/updateBI.html")
	public String updateBasicInformation(){
		return "redirect:/userAccountSettings.html";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/updateCI.html")
	public String updateContactInformation(){
		return "redirect:/userAccountSettings.html";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/updateSAC.html")
	public String updateSchoolsAndCompanies(){
		return "redirect:/userAccountSettings.html";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/updateHs.html")
	public String updateHobbies(){
		return "redirect:/userAccountSettings.html";
	}

	public void initLeftPage(BaseModel baseModel, HttpServletRequest request) {
		String username = getUserInSession();
		UserAccountSettingsModel model = (UserAccountSettingsModel) baseModel;
		
		model.setSchools(userAccountService.getUserSchools(username));
		model.setCompanies(userAccountService.getUserCompanies(username));
		model.setCategories(userAccountService.getUserCategories(username));
		model.setHobbies(userAccountService.getUserHobbies(username));
		model.setUserInfo(userAccountService.getUserInfo(username));
	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initTopZone(user, model);
		model.setUserId(user.getId());
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initRightZone(user, model);
		initPeopleYouMayKnow(user, model);
	}

}
